/*
 * Team Name: Team Sasquatch
 * Cody Sharff
 * Anthony Rios
 * Prince Kishore
 * Travis Drake
 */

package gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

/**
 * An observable GUI that will accept a user name and notify the observer
 * after the two have been entered.
 *
 */
public class StartUpGUI extends Observable {

    public static final String TITLE = "SECURE LOGIN";
    
    /**
     * The text field for data entry.
     */
    private final JTextField my_text = new JTextField(20);
    
    /**
     * 
     */
    private final JFrame my_frame = new JFrame();
    
    /**
     * The text field for data entry.
     */
    private final JTextField my_password = new JTextField(20);
    
    private final JButton my_login_button = new JButton("Login");
    
    private String my_login = "";
    
    private String my_password_text = "";
    
    /**
    * Constructs a new frame with components.
    */
    public StartUpGUI()
    {
	    my_frame.setTitle(TITLE);
	    my_frame.setResizable(false);
	    my_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        layoutComponents();
	}
    
    /**
     * Creates and arranges the components in the frame.
     */
    private void layoutComponents()
    {
      my_frame.setLocationRelativeTo(null);
      final JPanel north_panel = new JPanel(new FlowLayout());
      final JPanel center_panel = new JPanel(new FlowLayout());
      final JLabel id = new JLabel("ID           ");
      north_panel.add(id);
      north_panel.add(my_text);
      final JLabel password = new JLabel("Password");
      center_panel.add(password);
      center_panel.add(my_password);
      my_frame.add(north_panel, BorderLayout.NORTH);
      my_frame.add(center_panel, BorderLayout.CENTER);
      my_login_button.addActionListener(new loginListener());
      my_frame.add(my_login_button, BorderLayout.SOUTH);
      my_frame.getRootPane().setDefaultButton(my_login_button);
      my_frame.pack();
      
    }
    
    /**
     * Creates the event handlers (if necessary) and makes the
     * frame available for use.
     */
    public void start()
    {
      my_frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      my_frame.setVisible(true);
    }
    
    public String getLogin()
    {
    	return my_login;
    }
    
    public String getPassword()
    {
    	return my_password_text;
    }
    
    /**
     * use to dispose of the frame from another source.
     */
    public void dispose()
    {
        my_frame.dispose();
    }
    
    /**
     * displays an error if the login and or password are incorrect.
     */
    public void displayError()
    {
        JOptionPane.showMessageDialog(null, "incorrect login and/or password");
    }
    
    /**
     * an Action listener for the login button.
     * 
     * @author Anthony Rios
     * @version 11-28-11
     */
    protected class loginListener implements ActionListener
    {
      /**
       * 
       * @param the_event undo the previous drawing.
       */
      @Override
      public void actionPerformed(final ActionEvent the_event)
      {
          String login = my_text.getText();
          String password = my_password.getText();
          if (login.isEmpty() || password.isEmpty())
          {
        	  displayError();
          }
          else {
        	  my_login = login;
        	  my_password_text = password;
        	  setChanged();
        	  notifyObservers();
        	  
          }
        	  
      }
    }
}
